<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="店铺开通状态" prop="isOpen">
        <el-select v-model="queryParams.isOpen" placeholder="请选择店铺开通状态" clearable size="small">
          <el-option
            v-for="dict in isOpenOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店长姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入店长姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统计开始时间" prop="beginTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.beginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请输入开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="统计结束时间" prop="endTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请输入结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['suppliers:suppliers:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['suppliers:suppliers:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['suppliers:suppliers:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['suppliers:suppliers:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="suppliersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺名称" align="center" prop="supplierName" />
      <el-table-column label="店铺图标" align="center" prop="supplierIcon" width="107">
        <template slot-scope="scope">
          <img v-if="scope.row.supplierIcon" :src="scope.row.supplierIcon" alt="" width="100%" height="100%">
        </template>
      </el-table-column>
      <el-table-column label="店长姓名" align="center" prop="realName" />
      <el-table-column label="电话号码" align="center" prop="userPhone" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="店铺状态" align="center" prop="isCloseStore" :formatter="isCloseStoreFormat" />
      <el-table-column label="店铺开通状态" align="center" prop="isOpen" :formatter="isOpenFormat" />
      <el-table-column label="营业总额" align="center" prop="cost" />
      <el-table-column label="收入总额" align="center" prop="priceCost" />

      <el-table-column label="可提现" align="center" prop="shopSySumVo.ktxSum" />
      <el-table-column label="待结算" align="center" prop="shopSySumVo.djsSum" />
      <el-table-column label="店铺创建时间" align="center" prop="shopSySumVo.createTime" />

      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="openSupplierInfo(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['suppliers:suppliers:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['suppliers:suppliers:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商铺基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="唯一ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入唯一ID" />
        </el-form-item>
        <el-form-item label="是否有效">
          <el-select v-model="form.isActive" placeholder="请选择是否有效">
            <el-option
              v-for="dict in isActiveOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租户ID" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入租户ID" />
        </el-form-item>
        <el-form-item label="店铺状态">
          <el-select v-model="form.isCloseStore" placeholder="请选择店铺状态">
            <el-option
              v-for="dict in isCloseStoreOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺开通状态">
          <el-select v-model="form.isOpen" placeholder="请选择店铺开通状态">
            <el-option
              v-for="dict in isOpenOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺图标" prop="supplierIcon">
          <el-input v-model="form.supplierIcon" placeholder="请输入店铺图标" />
        </el-form-item>
        <el-form-item label="店长姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入店长姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer title="查看详情" :visible.sync="supplierInfoPop.open" direction="rtl" custom-class="supplier-pop-drawer" size="60%">
      <div class="supplier-pop">
        <div class="supplier-info">
          <div class="supplier-title">
            <img :src="supplierInfoPop.detail.supplierIcon" alt="">
            {{supplierInfoPop.detail.supplierName}}
          </div>
          <div class="supplier-detail">
            <div class="supplier-item">
              <div class="label">店长姓名：</div>
              <div class="value">{{supplierInfoPop.detail.realName||''}}</div>
            </div>
            <div class="supplier-item">
              <div class="label">手机号码：</div>
              <div class="value">{{supplierInfoPop.detail.userPhone||''}}</div>
            </div>
            <div class="supplier-item">
              <div class="label">身份证号：</div>
              <div class="value">{{supplierInfoPop.detail.idCard||''}}</div>
            </div>
            <div class="supplier-item">
              <div class="label">营业总额：</div>
              <div class="value" v-if="supplierInfoPop.detail.cost">{{supplierInfoPop.detail.cost||''}}元</div>
            </div>
            <div class="supplier-item">
              <div class="label">佣金收入：</div>
              <div class="value" v-if="supplierInfoPop.detail&&supplierInfoPop.detail.shopSySumVo">{{supplierInfoPop.detail.shopSySumVo.zzcSum||0}}元</div>
            </div>
            <div class="supplier-item">
              <div class="label">可提现：</div>
              <div class="value" v-if="supplierInfoPop.detail&&supplierInfoPop.detail.shopSySumVo">{{supplierInfoPop.detail.shopSySumVo.ktxSum||0}}元</div>
            </div>
            <div class="supplier-item">
              <div class="label">扫码核销：</div>
              <div class="value">{{supplierInfoPop.detail.isScan=='1'?'是':'否'}}</div>
            </div>
            <div class="supplier-item">
              <div class="label">店长开通状态：</div>
              <div class="value">{{supplierInfoPop.detail.isOpen=='1'?'开通':'未开通'}}</div>
            </div>
          </div>
        </div>
        <div class="supplier-order">
          <div class="title">店铺经营流水</div>
          <div class="order-list">
            <div class="order-item" v-for="item in supplierInfoPop.orders" :key="item.id">
              <div class="order-detail">
                <div class="order-detail-item">订单号：{{item.orderSn}}</div>
                <div class="order-detail-item">收货人：{{item.consignee}}</div>
                <div class="order-detail-item">手机号码：{{item.mobile}}</div>
                <div class="order-detail-item">地址：{{item.address}}</div>
                <div class="order-detail-item" v-if="item.invoiceNo">物流状态：<el-button size="mini" type="text" @click="toWl(item)">查看物流信息</el-button></div>
                <div class="order-detail-item">订单状态：{{item.orderStatusName}}</div>
              </div>
              <div class="order-goods-list">
                <el-row class="list-row-header" type="flex">
                  <el-col class="list-col" :span="4"></el-col>
                  <el-col class="list-col" :span="5">商品信息</el-col>
                  <el-col class="list-col" :span="5">售价</el-col>
                  <el-col class="list-col" :span="5">结算价</el-col>
                  <el-col class="list-col" :span="5">数量</el-col>
                </el-row>
                <el-row class="list-row" type="flex" v-for="citem in item.goods" :key="citem.id">
                  <el-col class="list-col" :span="4">
                    <img class="goods-img" :src="citem.goodsImg"/>
                  </el-col>
                  <el-col class="list-col" :span="5">
                    <div class="goods-name">{{citem.goodsName}}</div>
                    <div class="goods-attr">{{citem.goodsAttr}}</div>
                  </el-col>
                  <el-col class="list-col" :span="5">{{citem.price}}</el-col>
                  <el-col class="list-col" :span="5">{{citem.cost}}</el-col>
                  <el-col class="list-col" :span="5">{{citem.goodsNumber}}</el-col>

                </el-row>
                <div class="order-goods-count">
                  <div class="count-item">现金支付：¥{{item.moneyPaid||0}}</div>
                  <div class="count-item">积分支付：¥{{item.pointExPrice||0}}</div>
                  <div class="count-item">佣金：¥{{item.commission||0}}</div>
                </div>
              </div>
            </div>
          </div>
          <pagination
            v-show="supplierInfoPop.total>0"
            :total="supplierInfoPop.total"
            :page.sync="supplierInfoPop.pageNum"
            :limit.sync="supplierInfoPop.pageSize"
            @pagination="getOrderList"
          />
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listOrderInfo, getGoodsInfo } from "@/api/orderInfo/orderInfo";
import { shopSyTj } from "@/api/suppliers/withdrawal";
import { listSuppliers, getSuppliers, delSuppliers, updateSuppliers, exportSuppliers } from "@/api/suppliers/suppliers";

export default {
  name: "Suppliers",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商铺基本信息表格数据
      suppliersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      isActiveOptions: [],
      // 店铺状态字典
      isCloseStoreOptions: [],
      // 店铺开通状态字典
      isOpenOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isOpen: undefined,
        supplierName: undefined,
        realName: undefined,
        idCard: undefined,
        beginTime: undefined,
        endTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      supplierInfoPop:{
        open:false,
        id:'',
        detail:{},
        orders:[],
        pageNum:1,
        pageSize:10,
        total:0
      }
    };
  },
  created() {
    const that = this;
    this.getList();
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    that.getDicts("is_active").then(response => {
      that.isCloseStoreOptions = response.data;
      that.isCloseStoreOptions.forEach(function(item){
        if (item.dictLabel == '是') {
          item.dictLabel = '营业'
        } else {
          item.dictLabel = '未营业'
        }
      })
    });
    that.getDicts("is_active").then(response => {
      that.isOpenOptions = response.data;
      console.log('开通状态:');
      console.log(that.isOpenOptions);
      that.isOpenOptions.forEach(function(item){
        if (item.dictLabel == '是') {
          item.dictLabel = '开通'
        } else {
          item.dictLabel = '未开通'
        }
      })
    });
  },
  methods: {
    /** 查询商铺基本信息列表 */
    getList() {
      this.loading = true;
      listSuppliers(this.queryParams).then(response => {
        this.suppliersList = response.data || response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      });
    },
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    // 店铺状态字典翻译
    isCloseStoreFormat(row, column) {
      return this.selectDictLabel(this.isCloseStoreOptions, row.isCloseStore);
    },
    // 店铺开通状态字典翻译
    isOpenFormat(row, column) {
      return this.selectDictLabel(this.isOpenOptions, row.isOpen);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isActive: undefined,
        lesseeId: undefined,
        isCloseStore: undefined,
        isOpen: undefined,
        supplierName: undefined,
        supplierIcon: undefined,
        realName: undefined,
        idCard: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/LSY/LJCLXLX/suppliers/edit/new')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push('/LSY/LJCLXLX/suppliers/edit/'+(row.id||this.ids));
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加商铺基本信息";
    // },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getSuppliers(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改商铺基本信息";
    //   });
    // },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSuppliers(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSuppliers(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商铺基本信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSuppliers(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商铺基本信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSuppliers(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    getOrderList(){
      listOrderInfo({pageNum:this.supplierInfoPop.pageNum, pageSize:this.supplierInfoPop.pageSize, supplierId:this.supplierInfoPop.id}).then(response => {
        for(let i of response.rows){
          i.goods = []
          getGoodsInfo(i.id).then((res) => {
            if (res && res.code && res.code === 200) {
              if (res.rows) {
                i.goods = res.rows
              }
            }
          })
        }

        this.supplierInfoPop.orders = response.rows;
        this.supplierInfoPop.total = response.total;
      });
    },
    openSupplierInfo(row){
      const id = row.id
      this.supplierInfoPop.id = row.id
      this.supplierInfoPop.pageNum = 1
      this.supplierInfoPop.detail = row;
      /* getSuppliers(id).then(response => {

        shopSyTj({supplierId:id}).then(res => {
          this.supplierInfoPop.detail.shopSySumVo = res.data;
        });

      }); */
      this.supplierInfoPop.open = true;
      this.getOrderList(id)
    },
    toWl(row){
      this.$router.push({
        name: 'logisticDetail',
        params: {
          invoiceNo: row.invoiceNo,
          expressName: row.expressName
        }
      })
    }
  }
};
</script>
<style lang="scss" scoped>
  .pagination-container .el-pagination__total, .pagination-container .el-pagination__jump{
    color: #606266;
  }

  .el-drawer__wrapper /deep/ .supplier-pop-drawer{
      background-color: #F3F5F7;
  }
  .el-drawer__wrapper /deep/ .supplier-pop-drawer .el-drawer__header{
    background: #FFFFFF;
    border-bottom: 1px solid #E7E7E7;
    padding: 16px;
    font-size: 16px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(0,0,0,0.9);
    margin-bottom: 0;
  }
  .el-drawer__wrapper /deep/ .supplier-pop-drawer .el-drawer__body{
    height: 100%;
    overflow: auto;
  }
  .supplier-pop{
    .supplier-info{
      background-color: #FFFFFF;
      .supplier-title{
        display: flex;
        align-items: center;
        font-size: 20px;
        font-family: PingFang SC-Bold, PingFang SC;
        font-weight: bold;
        color: #000000;
        padding: 10px 24px;
        img{
          width: 100px;
          height: 100px;
          border-radius: 6px 6px 6px 6px;
          opacity: 1;
          border: 1px solid #F1F1F1;
          margin-right: 25px;
        }
      }
      .supplier-detail{
        display: flex;
        align-items: baseline;
        flex-wrap: wrap;
        padding: 0 17px 13px 17px;
        .supplier-item{
          display: flex;
          align-items: center;
          margin: 0 17px 10px 17px;
          flex: 0 0 calc(100% / 3 - 34px);
          font-size: 15px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #000000;
        }
      }

    }
    .supplier-order{
      margin-top: 10px;
      padding-bottom: 35px;
      background-color: #FFFFFF;
      .title{
        font-size: 18px;
        font-family: PingFang SC-Bold, PingFang SC;
        font-weight: bold;
        color: #000000;
        padding: 13px 18px;
      }
      .order-list{
        padding: 0 18px;
        .order-item{
          margin-bottom: 10px;
          border: 1px solid #EDEDED;
          .order-detail{
            background: #F1F8FF;
            border-radius: 0px 0px 0px 0px;
            opacity: 1;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 10px 10px 1px 10px;
            .order-detail-item{
              font-size: 14px;
              font-family: PingFang SC-Regular, PingFang SC;
              font-weight: 400;
              color: #333333;
              flex: 0 0 calc(100% / 3 - 14px);
              margin: 0 7px 10px 7px;
            }
          }
          .order-goods-list{

            .list-row-header{
              margin: 0 12px;
              align-items: center;
              .list-col{
                font-size: 13px;
                font-family: PingFang SC-Regular, PingFang SC;
                font-weight: 400;
                color: #666666;
                padding: 10px 0;
                text-align: center;
              }
            }
            .list-row{
              align-items: center;
              margin: 0 12px 8px 12px;
              background: #F8F8F8;
              border-radius: 3px 3px 3px 3px;
              opacity: 1;
              .list-col{
                font-size: 13px;
                font-family: PingFang SC-Regular, PingFang SC;
                font-weight: 400;
                color: #666666;
                padding: 10px 0;
                text-align: center;
                .goods-img{
                  width: 70px;
                  height: 70px;
                  border-radius: 3px 3px 3px 3px;
                  opacity: 1;
                  display: block;
                  margin: 0 auto;
                }
                .goods-name{
                  font-size: 15px;
                  font-family: PingFang SC-Bold, PingFang SC;
                  font-weight: bold;
                  color: #333333;
                  text-align: left;
                }
                .goods-attr{
                  text-align: left;
                  font-size: 12px;
                  font-family: PingFang SC-Regular, PingFang SC;
                  font-weight: 400;
                  color: #777777;
                  margin-top: 10px;
                }
              }
            }
            .order-goods-count{
              display: flex;
              align-items: center;
              justify-content: flex-end;
              padding: 10px;
              .count-item{
                margin-left: 38px;
                font-size: 15px;
                font-family: PingFang SC-Regular, PingFang SC;
                font-weight: 400;
                color: #000000;
              }
            }
          }
        }
      }
    }

  }





</style>
